using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using DigitalFortress.Entities;
using System.ServiceModel;
using DigitalFortress.Configuration;
using DigitalFortress.Fault;
using Castle.ActiveRecord;

namespace DigitalFortress.Testing.Agent.Administration.ApplicationOperations {

	/// <summary>
	/// Description 
	/// This method is responsible for retrieving all the applications in the security repository.
	/// This operation is only allowed for DigitalFortress.
	/// 
	/// Parameters 
	/// None. 
	/// Returns 
	/// Application[] - Returns all the applications. Returns an empty array if none is found. 
	/// Dependencies 
	/// 2.1 - Test successful creation. 
	/// Test Suite: 
	/// 1) Test successful retrieval.
	/// 2) Test successful retrieval with no applications.
	/// 3) Test retrieval of all the applications logged as 
	/// an application that's not DigitalFortress, 
	/// resulting in an expected fault (OperationNotAllowedFault).
	/// </summary>
	[TestFixture]
	public class RetrieveAllApplicationsTestSuite : BaseTestFixture {

		#region Fields
		AdministrationAgent agent = null;
		#endregion Fields

		#region Class Init

		/// <summary>
		/// Setups the test fixture.
		/// </summary>
		[TestFixtureSetUp]
		public override void TestFixtureSetUp() {
			base.TestFixtureSetUp();
			agent = new AdministrationAgent(XmlConfiguration.Current);
		}

		#endregion Class Init

		#region Initialize Data

		/// <summary>
		/// Initializes data for this test.
		/// </summary>
		public override void InitializeData() {

		}

		#endregion Initialize Data

		#region Tests

		/// <summary>
		/// Test successful retrieval.
		/// </summary>
		[Test]
		public void RetrieveAllApplicationsTest1() {
			Application appSample = ObjectMother.GetSampleApplication();
			appSample = agent.CreateApplication(appSample, "TestPassword");

			Application[] app = agent.RetrieveAllApplications();

			Assert.AreEqual(3, app.Length);
		}

		/// <summary>
		/// Test successful retrieval with no applications.
		/// </summary>
		[Test]
		public void RetrieveAllApplicationsTest2() {
			ActiveRecordStarter.DropSchema();
			ActiveRecordStarter.CreateSchema();
			Application[] app = agent.RetrieveAllApplications();

			Assert.AreEqual(0, app.Length);
		}

		/// <summary>
		/// Test retrieval of an application logged as an application that's not DigitalFortress, 
		/// resulting in an expected fault (OperationNotAllowedFault). 
		/// </summary>
		[Test, Ignore("No security implementation so far.")]
		public void RetrieveAllApplicationsTest3() {
			Application appSample = ObjectMother.GetSampleApplication();
			agent.CreateApplication(appSample, "TestPassword");

			Application[] app = GetAlternativeAgent().RetrieveAllApplications();
		}
		#endregion Tests

		#region Support Methods

		/// <summary>
		/// Gets the agent logged as an application other than DigitalFortress.
		/// </summary>
		/// <returns>Returns the logged agent</returns>
		private AdministrationAgent GetAlternativeAgent() {
			IConfiguration config = new CustomConfiguration(Guid.Empty, "fakePass");
			AdministrationAgent fakeAgent = new AdministrationAgent(config);

			return fakeAgent;
		}

		#endregion Support Methods

	}
}
